package com.scope.activities;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.scope.customViews.CustomView;
import com.scope.customViews.LabelButton;
import com.scope.R;

import org.opencv.android.OpenCVLoader;

public class HomeActivity extends CustomActivity {
    static {
        if (!OpenCVLoader.initDebug()) {
            Log.e("OpenCVLoader", "Unable to load...");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    private RelativeLayout mLayout;
    private void initializeLayout() {
        mLayout = new RelativeLayout(getApplicationContext());
        setContentView(
                mLayout,
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT)
        );
    }

    private LabelButton mGallery;
    private LabelButton mContact;
    private LabelButton mCamera;
    protected void initializeViews(Bundle savedInstanceState) {
        initializeLayout();

        mGallery = addNewButton();
        mGallery.setImageDrawable(getResources().getDrawable(R.drawable.icon_gallery));
        mGallery.setLabel(getResources().getString(R.string.gallery_label));
        mGallery.setId(1);
        mGallery.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(HomeActivity.this, GalleryActivity.class));
            }
        });

        mContact = addNewButton();
        mContact.setImageDrawable(getResources().getDrawable(R.drawable.icon_contact));
        mContact.setLabel(getResources().getString(R.string.contact_label));

        mCamera = addNewButton();
        mCamera.setImageDrawable(getResources().getDrawable(R.drawable.icon_camera));
        mCamera.setLabel(getResources().getString(R.string.camera_label));
    }

    private LabelButton addNewButton() {
        LabelButton btn = new LabelButton(getApplicationContext());

        btn.setOrientation(CustomView.Orientation.VERTICAL);
//        btn.setImageSize(getResources().getDimensionPixelSize(R.dimen.icon_width_v_l),
//                getResources().getDimensionPixelSize(R.dimen.icon_height_v_l));
//        btn.setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_v_l));
//        btn.setLabelVisibility(true);
        btn.setAccent(getResources().getColor(R.color.Green));
        btn.setClickable(true);
        btn.setBackgroundResource(R.drawable.background_selectable);

        RelativeLayout.LayoutParams lp =
                new RelativeLayout.LayoutParams(
                        getResources().getDimensionPixelSize(R.dimen.button_width_v_l),
                        getResources().getDimensionPixelSize(R.dimen.button_height_v_l));
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);

        mLayout.addView(btn, lp);

        return btn;
    }

    protected void positionViews() {
        mLayout.post(new Runnable() {
            @Override
            public void run() {
                Point size = new Point();
                CustomView.StaticMethods.ScreenSizeLayout(getWindow(), size);
            }
        });

        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);

        RelativeLayout.LayoutParams cameralp =
                new RelativeLayout.LayoutParams(
                        getResources().getDimensionPixelSize(R.dimen.button_width_v_l),
                        getResources().getDimensionPixelSize(R.dimen.button_height_v_l));

        RelativeLayout.LayoutParams contactlp =
                new RelativeLayout.LayoutParams(
                        getResources().getDimensionPixelSize(R.dimen.button_width_v_l),
                        getResources().getDimensionPixelSize(R.dimen.button_height_v_l));

        if (size.x < size.y) {
            cameralp.addRule(RelativeLayout.CENTER_HORIZONTAL);
            cameralp.addRule(RelativeLayout.ABOVE, mGallery.getId());
            contactlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
            contactlp.addRule(RelativeLayout.BELOW, mGallery.getId());
        } else {
            cameralp.addRule(RelativeLayout.CENTER_VERTICAL);
            cameralp.addRule(RelativeLayout.LEFT_OF, mGallery.getId());
            contactlp.addRule(RelativeLayout.CENTER_VERTICAL);
            contactlp.addRule(RelativeLayout.RIGHT_OF, mGallery.getId());
        }

        mCamera.setLayoutParams(cameralp);
        mContact.setLayoutParams(contactlp);
    }

    public void setAccent(int accent) {
        mAccent = accent;
        CustomView.StaticMethods.setAccent(mLayout, accent);
    }
}
